package com.yunfei.api.v2;

import com.yunfei.api.v1.magerIndcInfo.service.MagerIndcInfoService;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.contants.AppKeyContant;
import com.yunfei.api.v2.contants.QueryHeTongSignInfoContant;
import com.yunfei.api.v2.enums.CancelElecHtEnum;
import com.yunfei.api.v2.enums.CreateZuoFeiXieYiEnum;
import com.yunfei.api.v2.enums.GetAccountInfoEnum;
import com.yunfei.api.v2.param.*;
import com.yunfei.api.v2.param.faQiQianShu.FaQiDianZiHeTongQianShuParam;
import com.yunfei.api.v2.param.heTongSign.HeTongSignParam;
import com.yunfei.api.v2.result.CreateElecHtResult;
import com.yunfei.api.v2.result.ElecHeTongNoResult;
import com.yunfei.api.v2.result.HeTongSignInfoGetStatusResult;
import com.yunfei.api.v2.result.LookHeTongDetailJson.LookHeTongDetailJson;
import com.yunfei.api.v2.result.LookHeTongSignStatusJson.LookHeTongSignStatusJson;
import com.yunfei.api.v2.result.QiXiaoDianZiQianShuIsCancel.QuXiaoQianShuLiuChengIsCancelResult;
import com.yunfei.api.v2.result.YuYueInfoResult;
import com.yunfei.api.v2.result.createTemplate.CreateTemplateJsonRootBean;
import com.yunfei.api.v2.result.heTongSign.HeTongSignResult;
import com.yunfei.api.v2.result.jiaGaiResult.AutoJiaGaiResult;
import com.yunfei.api.v2.result.zuoFeiXieYiSign.ZuoFeiXieYiSignResult;
import com.yunfei.api.v2.service.*;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.service.ValidMobileAndCodeService;
import com.yunfei.utils.http.HttpIOUtil;
import com.yunfei.utils.http.JsonParamHttpUtil;
import com.yunfei.utils.http.QueryHeTongStatusHttpUtil;
import com.yunfei.utils.json.JsonUtil;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.heTongSignManager.entity.HeTongSignManagerEntity;
import com.yunfei.yunhetong.magerIndeInfo.entity.MagerIndcInfoEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 系统生成电子合同
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/v2/elecHt")
@Api(value = QueryHeTongSignInfoContant.ELEC_HT_NAMESPACE,tags =  QueryHeTongSignInfoContant.ELEC_HT_NAMESPACE )
public class ElecHtApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private GetStatusMapper getStatusMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private ValidMobileAndCodeService validMobileAndCodeService;
    @Autowired
    private Environment env;
    @Autowired
    private MagerIndcInfoMapper magerIndcInfoMapper;
    @Autowired
    private MagerIndcInfoTemplateMapper magerIndcInfoTemplateMapper;
    @Autowired
    private MagerIndcInfoService magerIndcInfoService;
    @Autowired
    private HTSGetStatusService htsGetStatusService;
    @Autowired
    private HTSGetDetailService htsGetDetailService;
    @Autowired
    private HTSGetAccountInfoService htsGetAccountInfoService;
    @Autowired
    private HTSAddAccountService htsAddAccountService;
    @Autowired
    private HTYuYueInfoService htYuYueInfoService;
    @Autowired
    private GetElecHeTongNoService getElecHeTongNoService;
    @Autowired
    private CreateElecHtService createElecHtService;
    @Autowired
    private HTSFaQiQianShuService htsFaQiQianShuService;
    @Autowired
    private HTSQuXiaoQianShuService htsQuXiaoQianShuService;
    @Autowired
    private ContractSignService contractSignService;
    /**
     * @Description: 系统生成电子合同
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/create")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.CREATE_ELEC_HT_NAMESPACE,notes = QueryHeTongSignInfoContant.CREATE_ELEC_HT_NAMESPACE,response = CreateTemplateJsonRootBean.class)
    public Object create(ReserveNoParam param, CustNoParam custNoParam,ProjIdParam projIdParam){
        String appId=env.getProperty(AppKeyContant.appId);
        String appKey=env.getProperty(AppKeyContant.appKey);
        HttpIOUtil httpIOUtil=new HttpIOUtil();
        String token= httpIOUtil.getLongToken(appId,appKey,"");
        return createElecHtService.create(token,param,projIdParam,custNoParam);
    }
    /**
     * @Description: 作废电子合同
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/zuoFei")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.CANCLE_ELEC_HT_NAMESPACE,notes = QueryHeTongSignInfoContant.CANCLE_ELEC_HT_NAMESPACE,response = ResultModel.class)
    public Object create(ReserveNoParam param){
        return createElecHtService.cancel(param);
    }

    /**
     * @Description: 获取电子合同合同编号
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/getNo")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.GET_ELEC_CONTCON_NO,notes = QueryHeTongSignInfoContant.GET_ELEC_CONTCON_NO,response = ElecHeTongNoResult.class)
    @ResponseBody
    public Object getEhtNo(ProjIdParam param){
        return getElecHeTongNoService.getEhtNo(param);
    }
    /**
     * @Description: 获取电子合同合同编号
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/getYuYueInfo")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.GET_YUYUE_INFO,notes = QueryHeTongSignInfoContant.GET_YUYUE_INFO,response = ElecHeTongNoResult.class)
    @ResponseBody
    public Object getYuYueInfo(ReserveNoParam param){
        return htYuYueInfoService.get(param);
    }
    /**
     * @Description:发起电子合同签署流程（理财师）
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/faQiDianZiHeTongQianShuLiuCheng")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.FA_QI_DIANZI_HETONG_QIANSHU_LIUCHENG,notes = QueryHeTongSignInfoContant.FA_QI_DIANZI_HETONG_QIANSHU_LIUCHENG,response = ResultModel.class)
    @ResponseBody
    public Object faQiDianZiHeTongQianShuLiuCheng(FaQiDianZiHeTongQianShuParam param){
        return htsFaQiQianShuService.faQiDianZiHeTongQianShuLiuCheng(param);
    }
    /**
     * @Description:判断是否可取消（用户未录入账号信息前可取消，录入账号后可作废）
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/quXiaoDianZiHeTongQianShuLiuCheng/isCancel")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.QUXIAO_DIANZI_HETONG_QIANSHU_LIUCHENG+QueryHeTongSignInfoContant.IS_CANCEL,notes = QueryHeTongSignInfoContant.QUXIAO_DIANZI_HETONG_QIANSHU_LIUCHENG+QueryHeTongSignInfoContant.IS_CANCEL,response = QuXiaoQianShuLiuChengIsCancelResult.class)
    @ResponseBody
    public Object quXiaoDianZiHeTongQianShuLiuCheng(ReserveNoParam param){
        return htsQuXiaoQianShuService.isCancel(param);
    }
    /**
     * @Description:取消逻辑
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/quXiaoDianZiHeTongQianShuLiuCheng/cancel")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.QUXIAO_DIANZI_HETONG_QIANSHU_LIUCHENG+QueryHeTongSignInfoContant.CANCEL,notes = QueryHeTongSignInfoContant.QUXIAO_DIANZI_HETONG_QIANSHU_LIUCHENG+QueryHeTongSignInfoContant.CANCEL,response = ResultModel.class)
    @ResponseBody
    public Object cancel(ReserveNoParam param){
        return htsQuXiaoQianShuService.cancel(param);
    }
    /**
     * @Description:作废协议签署功能判断
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/zuoFei/sign")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.ZUOFEI_XIEYI_SIGN,notes = QueryHeTongSignInfoContant.ZUOFEI_XIEYI_SIGN_NOTES,response = ZuoFeiXieYiSignResult.class)
    @ResponseBody
    public Object zuofeiSign(ReserveNoParam param){
        return htsQuXiaoQianShuService.zuoFeiSignPanDuan(param);
    }
    /**
     * @Description:添加签署作废协议
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/sign/zuoFeiXieYi")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.SIGN_ZUOFEI_XIEYI,notes = QueryHeTongSignInfoContant.SIGN_ZUOFEI_XIEYI,response = ResultModel.class)
    @ResponseBody
    public Object signZuoFeiXieYi(SignZuoFeiXieYiParam param){
        return htsQuXiaoQianShuService.signZuoFeiXieYi(param);
    }
    /**
     * @Description: 查询电子合同签署状态
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/getSignStatus")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.GET_STATUS,notes = QueryHeTongSignInfoContant.GET_STATUS_NOTES,response = HeTongSignInfoGetStatusResult.class)
    @ResponseBody
    public Object getStatus(HeTongSignInfoGetStatusParam param){
        return htsGetStatusService.getStatus(param);
    }
    /**
     * @Description: 查询电子合同签署状态
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/detail")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.GET_HT_DETAIL,notes = QueryHeTongSignInfoContant.GET_HT_DETAIL_NOTES)
    @ResponseBody
    public Object detail(ContractIdParam param){
        return htsGetDetailService.getDetail(param);
    }
     /**
     * @Description: 根据预约编号查询云合同的电子合同ID
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/getContractId")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.GET_HT_ID,notes = QueryHeTongSignInfoContant.GET_HT_ID)
    @ResponseBody
    public Object getContractId(ReserveNoParam param){
        return htsGetDetailService.getContractId(param);
    }
    /**
     * @Description: 根据预约编号查询云合同的电子合同ID
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/getSignerId")
    @ApiOperationSort(value = 1)
    @ApiOperation(position = 1,value = QueryHeTongSignInfoContant.GET_SIGNER_ID,notes = QueryHeTongSignInfoContant.GET_SIGNER_ID)
    @ResponseBody
    public Object getSignerId(CustNoParam param){
        return htsGetDetailService.getSignerId(param);
    }

    /**
     * @Description: 签署合同
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/sign")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.SING_HETONG_SAVE,notes = QueryHeTongSignInfoContant.SING_HETONG_NOTES,response = HeTongSignResult.class)
    @ResponseBody
    public Object signContract(HeTongSignParam param){
        ResultModel resultModel=new ResultModel();
        String idType=param.getIdType();//参数类型：0 合同 ID，1 合同自定义编号
        //设置签章样式，sealClass =3，含签署时间的样式
        param.setSealClass("3");
        if(StringUtil.isNullOrEmpty(idType)){
            param.setIdType("0");
        }
        resultModel=contractSignService.sign(param);
        int code=resultModel.getCode();
        System.out.println("合同签署完成返回结果。。。。。。。。。。。。。。code="+code);
        if(code==0){
            //更新合同签署状态
            HeTongSignManagerEntity updateStatusParam=new HeTongSignManagerEntity();
            updateStatusParam.setContractId(param.getIdContent());
            updateStatusParam.setContractSts("2");
            contractSignService.updateContractSignStatus(updateStatusParam);
        }
        //投资者签署完成，调用执行更新合同状态
        return resultModel;
    }    /**
     * @Description: 更新合同签署状态
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/updateContractSts")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.UPDATE_CONTRACT_SIGN_STATUS,notes = QueryHeTongSignInfoContant.UPDATE_CONTRACT_SIGN_STATUS,response = HeTongSignResult.class)
    @ResponseBody
    public Object updateContractSts(ContractIdParam param){
        ResultModel resultModel=new ResultModel();
        //更新合同签署状态
        HeTongSignManagerEntity updateStatusParam=new HeTongSignManagerEntity();
        updateStatusParam.setContractId(param.getContractId());
        if(StringUtil.isNullOrEmpty(param.getContractId())){
            resultModel.setCode(CreateZuoFeiXieYiEnum.CONTRACT_ID_NOTNULL.getCode());
            resultModel.setMessage(CreateZuoFeiXieYiEnum.CONTRACT_ID_NOTNULL.getMessage());
            return resultModel;
        }
        updateStatusParam.setContractSts("2");
        try{
            contractSignService.updateContractSignStatus(updateStatusParam);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }

        return resultModel;
    }

}
